package com.shop.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 字符串脱敏工具类
 *
 * @author Jerry
 */
public class StrMaskUtil {

    /**
     * 手机号脱敏
     *
     * @param mobile
     * @return
     */
    public static String maskMobile(String mobile) {
        if(StringUtils.isNotBlank(mobile)) {
            return mobile.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2");
        }
        return mobile;
    }

    /**
     * 真实姓名脱敏
     *
     * @param realityName
     * @return
     */
    public static String maskRealityName(String realityName) {
        if(StringUtils.isNotBlank(realityName)) {
            // return StringUtils.rightPad(StringUtils.left(realityName, 1), StringUtils.length(realityName), "*");
            int len = realityName.length();
            if(len == 1) {
                return realityName;
            } else if(len == 2) {
                return realityName.replaceFirst(realityName.substring(1), "*");
            } else {
                return realityName.replaceFirst(realityName.substring(1, len - 1), "*");
            }
        }
        return realityName;
    }

    /**
     * 身份证号脱敏
     *
     * @param idNumber
     * @return
     */
    public static String maskIdNumber(String idNumber) {
        if(StringUtils.isNotBlank(idNumber)) {
            if (idNumber.length() == 15){
                idNumber = idNumber.replaceAll("(\\w{6})\\w*(\\w{4})", "$1******$2");
            }
            if (idNumber.length() == 18){
                idNumber = idNumber.replaceAll("(\\w{6})\\w*(\\w{4})", "$1*********$2");
            }
        }
        return idNumber;
    }



}
